package com.dfdchem.imd.kaiwu.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.model.SysMessageVo;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.util.RequestUtil;
import com.dfdchem.imd.kaiwu.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class SysMessageAdapter extends RecyclerView.Adapter<SysMessageAdapter.ViewHolder> {

    private final static String TAG = "UserMessageAdapter";

    private AppCompatActivity context;

    private SimpleDateFormat sdf;

    @SuppressLint("SimpleDateFormat")
    public SysMessageAdapter(AppCompatActivity context) {
        this.context = context;
        sdf = new SimpleDateFormat("yyyy-MM-dd");
    }

    private List<SysMessageVo> messages;

    public List<SysMessageVo> getMessages() {
        return messages;
    }

    public void setMessages(List<SysMessageVo> messages) {
        this.messages = messages;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView avatar;
        public TextView message_from;
        public TextView send_time;
        public TextView summary;
        public TextView unread_count;

        public ViewHolder(View view) {
            super(view);
            avatar = view.findViewById(R.id.avatar);
            message_from = view.findViewById(R.id.message_from);
            send_time = view.findViewById(R.id.send_time);
            summary = view.findViewById(R.id.summary);
            unread_count = view.findViewById(R.id.unread_count);
        }
    }

    @Override
    public SysMessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sys_message, parent, false);

        return new ViewHolder(view);
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SysMessageVo message = messages.get(position);

        User user = message.getFromUser();
        if (user != null) {
            holder.message_from.setText(message.getFromUser().getUserName());
            String avatar_url = message.getFromUser().getAvatar();
            if (StringUtil.isEmpty(avatar_url)) {
                holder.avatar.setImageDrawable(context.getDrawable(R.drawable.ic_default_avatar));
            } else {
                // 适配内网地址
                if (avatar_url.startsWith(RequestUtil.url_file_short)) {
                    avatar_url = RequestUtil.URL_BASE_API + avatar_url;
                }

                new GlideManager(context).load(holder.avatar, avatar_url);
            }
        } else {
            holder.message_from.setText("已注销");
            holder.avatar.setImageDrawable(context.getDrawable(R.drawable.ic_default_avatar));
        }

        holder.send_time.setText(date2String(message.getLastMessage().getSendTime()));
        holder.summary.setText(message.getLastMessage().getContent());
        holder.unread_count.setText(message.getUnreadCount() + "");
        holder.unread_count.setVisibility(message.getUnreadCount() > 0 ? View.VISIBLE:View.GONE);
    }

    @Override
    public int getItemCount() {
        return messages == null ? 0 : messages.size();
    }

    private String date2String(Date date){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        Calendar nowCalendar = new GregorianCalendar();
        nowCalendar.setTime(new Date());

        String dateStr = sdf.format(date);
        if (nowCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
            dateStr = dateStr.substring(5);
        }
        return dateStr;
    }
}
